﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ThreadV1_Version1._1
{
    /*
     */
    class Program
    {
        static void Main(string[] args)
        {

            //基本方式
            System.Threading.Thread thread = new System.Threading.Thread(DoJob);
            thread.Start();

            //传参数的方式
            System.Threading.ParameterizedThreadStart p = new System.Threading.ParameterizedThreadStart(DoParamJob);
            System.Threading.Thread paramthread = new System.Threading.Thread(p);
            paramthread.Start(10);

            //委托基本方式
            System.Threading.Thread delegateThread = new System.Threading.Thread(delegate()
            {
                DoJob();
            });
            delegateThread.Start();

            //委托传参数方式
            System.Threading.Thread delegateParamThread = new System.Threading.Thread(delegate(object para)
            {
                DoParamJob(para);
            });
            delegateParamThread.Start(10);


            //表达式的方式
            System.Threading.Thread lambdarThread = new System.Threading.Thread(() =>
            {
                DoJob();
            });
            lambdarThread.Start();

            //委托传参数表达式方式
            System.Threading.Thread lambdarParamThread = new System.Threading.Thread((object para) =>
            {
                DoParamJob(para);
            });
            lambdarParamThread.Start(10);

            //线程池
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object o) { DoJob(); }));
            System.Threading.ThreadPool.QueueUserWorkItem(DoParamJob,10);

            Console.ReadLine();
        }

        private static void DoJob()
        {
            Console.WriteLine("开始处理...");
            for (var i = 0; i < 10; i++)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("线程：{0}，第{1}次运算", System.Threading.Thread.CurrentThread.ManagedThreadId, i + 1);
            }
            Console.WriteLine("处理结束...");
        }


        private static void DoParamJob(object para)
        {
            Console.WriteLine("开始处理...");
            for (var i = 0; i < (int)para; i++)
            {
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("线程：{0}，第{1}次运算", System.Threading.Thread.CurrentThread.ManagedThreadId, i + 1);
            }
            Console.WriteLine("处理结束...");
        }
    }
}
